//创建折线图的option [不得修改]
function createLineOption(options) {
	return option = {
		title: {
			text: options.title || '',
			subtext: options.subtext || '',
		},
		color: ['#c23531', '#2f4554', '#61a0a8', '#d48265', '#91c7ae', '#749f83', '#ca8622', '#bda29a', '#6e7074', '#546570',
			'#c4ccd3'
		],
		tooltip: {
			trigger: 'axis',
			axisPointer: {
				type: 'cross',
				label: {
					backgroundColor: '#6a7985'
				}
			}
		},
		legend: {
			data: options.legendData || []
		},
		grid: {
			left: '3%',
			right: '4%',
			bottom: '3%',
			containLabel: true
		},
		xAxis: [{
			type: 'category',
			boundaryGap: false,
			data: options.xAxisData || []
		}],
		yAxis: [{
			type: 'value'
		}],
		series: options.series || []
	};

}


//创建条形图的option [不得修改]
function createBarOption(options) {
	return option = {
		color: ['#c23531', '#2f4554', '#61a0a8', '#d48265', '#91c7ae', '#749f83', '#ca8622', '#bda29a', '#6e7074', '#546570',
			'#c4ccd3'
		],
		title: {
			text: options.title || '',
			subtext: options.subtext || '',
		},
		tooltip: {
			trigger: 'axis',
			axisPointer: { // 坐标轴指示器，坐标轴触发有效
				type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
			}
		},
		legend: {
			data: options.legendData || []
		},
		grid: {
			left: '3%',
			right: '4%',
			bottom: '3%',
			containLabel: true
		},
		xAxis: [{
			type: 'category',
			data: options.xAxisData || []
		}],
		yAxis: [{
			type: 'value'
		}],
		series: options.series || []
	};

}

//创建pie饼图的option [不得修改]
function createPieOption(options) {
	return option = {
		color: ['#c23531', '#2f4554', '#61a0a8', '#d48265', '#91c7ae', '#749f83', '#ca8622', '#bda29a', '#6e7074', '#546570',
			'#c4ccd3'
		],
		title: {
			text: options.title || '',
			subtext: options.subtext || '',
			left: 'center'
		},
		tooltip: {
			trigger: 'item',
			formatter: '{a} <br/>{b} : {c} ({d}%)'
		},
		legend: {
			bottom: 10,
			left: 'center',
			data: options.legendData || []
		},
		series: getDefaultPieSeries(options),
	};
}

/**
 * 获取饼形图 series的默认参数
 * @param {Object} options
 */
function getDefaultPieSeries(options) {
	options.series = options.series || [{}];
	return [{
		type: 'pie',
		radius: '65%',
		center: ['50%', '50%'],
		selectedMode: 'single',
		data: options.series[0].data,
		emphasis: {
			itemStyle: {
				shadowBlur: 10,
				shadowOffsetX: 0,
				shadowColor: 'rgba(0, 0, 0, 0.5)'
			}
		}
	}];
}

/**@retrun myChart
 * @param {Object} domId 绘图dom的id
 * @param {Object} option echart 配置参数
 * @param {Object} notMerge 是否重新绘制 为false 则会不合并
 */
function initEcharts(domId, option, notMerge) {
	notMerge = notMerge || true;
	var dom = document.getElementById(domId);
	var myChart = echarts.init(dom);
	myChart.setOption(option, notMerge);
	return myChart;
}


/**
 * 主要就是操作x或y轴文字的展示问题 和滚动条
 * @retrun options
 * @param {Object} options  原optionx
 * @param {Object} axis 操作x轴还是y轴("x" or ""y)
 * @param {Object} textTilt 文字是否倾斜显示
 * @param {Object} linefeed 文字是否换行展示(默认是5个字符换行)
 * @param {Object} needZoom 是否需要滚动条
 * @param {Object} needToolbox 是否工具箱
 */
function setExtraOption(options, axis, textTilt, linefeed, needZoom, needToolbox) {
	axis = (axis != 'x' || axis != 'y') ? "x" : axis;
	var x_y = axis.toLowerCase() + "Axis";

	needZoom = typeof(needZoom) == 'boolean' ? needZoom : true;

	if (options[x_y] && options[x_y].constructor === Array) {
		if (textTilt) {
			options[x_y][0].axisLabel = {
				interval: 0,
				rotate: 38
			}
		}
		if (linefeed) {
			options[x_y][0].axisLabel = options[x_y][0].axisLabel || {};
			options[x_y][0].axisLabel.formatter = function(value) {
				return value.replace(/(.{5})/g, '$1\n')
			};
		}
	}
	if (options[x_y] && options[x_y].constructor === Object) {
		if (textTilt) {
			options[x_y].axisLabel = {
				interval: 0,
				rotate: 38
			}
		}
		if (linefeed) {
			options[x_y].axisLabel = options[x_y].axisLabel || {};
			options[x_y].axisLabel.formatter = function(value) {
				return value.replace(/(.{5})/g, '$1\n')
			};
		}
	}
	//x轴的滚动条
	if (axis == 'x' && needZoom) {
		options.dataZoom = [{
			id: 'dataZoomX',
			type: 'slider',
			xAxisIndex: [0],
			filterMode: 'filter',
			showDetail: false,
			maxValueSpan: 11, //最多12个
			start: 0,
			// end:100,
			bottom: -3,
			height: 10
		}];
	}
	//y轴的滚动条
	if (axis == 'y' && needZoom) {
		options.dataZoom = [{
			id: 'dataZoomY',
			type: 'slider',
			yAxisIndex: [0],
			filterMode: 'filter',
			showDetail: false,
			maxValueSpan: 11, //最多12个
			start: 0,
			// end:80,
			right: '3%',
			width: 10
		}];
	}
	if (needToolbox) {
		options.toolbox = {
			feature: {
				dataView: {
					readOnly: false
				},
				restore: {},
				saveAsImage: {}
			}
		};
	}
	return options;
}


//创建漏斗图的option [不得修改]
function createFunnelOption(options) {
	return option = {
		title: {
			text: options.text || '',
			subtext: options.subtext || ''
		},
		tooltip: {
			trigger: 'item',
			formatter: "{a} <br/>{b} : {c}%"
		},
		legend: {
			data: options.legendData
		},
		series: [{
			name: options.series[0].name || '',
			type: 'funnel',
			left: '10%',
			top: 60,
			//x2: 80,
			bottom: 60,
			width: '80%',
			// height: {totalHeight} - y - y2,
			min: 0,
			max: 100,
			minSize: '0%',
			maxSize: '100%',
			sort: 'descending',
			gap: 2,
			label: {
				show: true,
				position: 'inside'
			},
			labelLine: {
				length: 10,
				lineStyle: {
					width: 1,
					type: 'solid'
				}
			},
			itemStyle: {
				borderColor: '#fff',
				borderWidth: 1
			},
			emphasis: {
				label: {
					fontSize: 20
				}
			},
			data: options.series[0].data || []
		}]
	};
}


//创建仪表图的option [不得修改]
function createGaugeOption(options) {
	return option = {
		title: {
			text: options.text || '',
			subtext: options.subtext || ''
		},
		tooltip: {
			formatter: '{a} <br/>{b} : {c}%'
		},
		series: [{
			name: options.series[0].name||'',
			type: 'gauge',
			detail: {
				formatter: '{value}%'
			},
			data:  options.series[0].data||[]
		}]
	};

}


//创建热力图2D的option [不得修改]
function createHeatMap2DOption(options) {
	return option = {
		"title": {
			"text": options.title,
			"subtext":options.subtext,
			"left": "center"
		},
		"tooltip": {
		},
		"xAxis": {
			"type": "category",
			"data": options.xAxisData
		},
		"yAxis": {
			// "name": "01排",
			"nameLocation": "center",
			"nameRotate": 0,
			"nameGap": 35,
			"type": "category",
			"data": options.yAxisData
		},
		//dataZoom 可修改
		"dataZoom": [{
			"type": "slider",
			"xAxisIndex": [0],
			"start": 0,
			"end": 125
		}, {
			"type": "slider",
			"yAxisIndex": [0],
			"start": 0,
			"left": "93%",
			"end": 100
		}, {
			"type": "inside",
			"xAxisIndex": [0],
			"start": 0,
			"end": 125
		}, {
			"type": "inside",
			"yAxisIndex": [0],
			"start": 0,
			"end": 100
		}],
		"visualMap": {
			"show": false
		},
		"series": [{
			// "name": "仓位",
			"type": "heatmap",
			coordinateSystem: options.coordinateSystem||'cartesian2d',
			"data": options.seriesData||[],
			"label": {
				"normal": {
					"show": true,
					// 'formatter': function(p) {
					// },
					"rich": {
						"white": {
							"align": "right",
							"color": "#ffffff"
						},
						"location": {
							"fontSize": 14,
							"lineHeight": 20,
							"color": "#ffffff"
						}
					}
				}
			},
			"itemStyle": {
				"emphasis": {
					"shadowBlur": 10,
					"shadowColor": "rgba(0, 0, 0, 0.5)"
				}
			}
		}]
	};

}


//创建热力图[日历]的option [不得修改]
function createHeatMapCalendarOption(options) {
	return option ={
		    title: {
		        top: 30,
		        left: 'center',
		        text: options.title||''
		    },
		    tooltip: {},
		    visualMap: {
		        min: 0,
		        max: 10000,
		        type: 'piecewise',
		        orient: 'horizontal',
		        left: 'center',
		        top: 65,
		        textStyle: {
		            color: '#000'
		        }
		    },
		    calendar: {
		        top: 120,
		        left: 30,
		        right: 30,
		        cellSize: ['auto', 13],
		        range: options.calendarRange||'2020',
		        itemStyle: {
		            borderWidth: 0.5
		        },
		        yearLabel: {show: false}
		    },
		    series: {
		        type: 'heatmap',
		        coordinateSystem: options.coordinateSystem,
		        data: options.seriesData || []
		    }
		};
}
